<?php
/**
 * Created by PhpStorm.
 * User: 87976
 * Date: 2018/1/18
 * Time: 11:18
 */

namespace App\Services\HomeTemplates;


use App\Adapters\HomeAdapter;
use App\Repositories\HomeTemplates\HomeTemplateRepository;
use App\Repositories\HomeTemplates\HomeTemplateStencilRepository;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Class HomeService
 * @package App\Services
 */
class HomeService
{

    /**
     * @var HomeTemplateRepository
     */
    protected $homeTemplateRepository;

    /**
     * @var HomeTemplateStencilRepository
     */
    protected $homeTemplateStencilRepository;

    /**
     * HomeService constructor.
     * @param HomeTemplateRepository $homeTemplateRepository
     * @param HomeTemplateStencilRepository $homeTemplateStencilRepository
     */
    public function __construct(HomeTemplateRepository $homeTemplateRepository, HomeTemplateStencilRepository $homeTemplateStencilRepository)
    {
        $this->homeTemplateRepository = $homeTemplateRepository;

        $this->homeTemplateStencilRepository = $homeTemplateStencilRepository;
    }

    /**
     * 处理首页数据
     * @author Jerry
     * @param int $id
     * @param int $isShow
     * @return mixed
     */
    public function handle(int $id, int $isShow)
    {
        if ($isShow) {
            $home = $this->getTestHome($id);
        }
        else{
            $home = $this->getHome($id);
        }

        $data = $this->templateStencils($home['data']);

        $homeName = $this->getHomeName($id, $isShow);

        $home['data'] = $data;
        $home['name'] = $homeName;

        return $home;
    }

    /**
     * 获取首页名称
     * @author Jerry
     * @param int $id
     * @param int $isShow
     * @return mixed
     */
    protected function getHomeName(int $id, int $isShow)
    {

        if ($isShow) {
            $group = $this->findHomeTemplateById($id);
        }
        else{
            $group = $this->getHomeGroup($id);
        }

        return $group->name;
    }

    /**
     * 循环处理模块数据
     * @author Jerry
     * @param array $stencil
     * @return array
     */
    protected function templateStencils(array $stencil)
    {
        if (empty($stencil)) {
            throw new NotFoundHttpException('处理模块失败，模块数据为空！');
        }

        $data = [];

        foreach ($stencil as $key => $value) {
            $template = $this->stencil($value['type'])->handle($value);

            if (empty($template)) {
                continue;
            }

            $template['border_size'] = 20;

            $data[] = $template;
        }

        return $data;
    }

    /**
     * 更具类型调起适配器处理模版
     * @author Jerry
     * @param int $type
     * @return HomeAdapter
     */
    protected function stencil(int $type)
    {
        return new HomeAdapter($type);
    }

    /**
     * 获取测试首页
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    protected function getTestHome(int $id)
    {
        try{
            $homeTemplate = $this->findHomeTemplateById($id);
        }catch (\Exception $exception) {
            throw new NotFoundHttpException('获取首页测试数据失败');
        }

        return $this->findHomeStencils($homeTemplate->id);

    }

    /**
     * 获取指定id首页模块
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    protected function findHomeTemplateById(int $id)
    {
        return $this->homeTemplateRepository->find($id);
    }

    /**
     * 获取首页数据
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    protected function getHome(int $id)
    {
        try{
            $homeGroup = $this->getHomeGroup($id);
        }catch (\Exception $exception) {
            throw new NotFoundHttpException('获取首页数据失败');
        }


        $homeTemplate = $this->getHomeParentById($homeGroup->id);

        if (empty($homeTemplate)) {
            throw new NotFoundHttpException('获取首页分组数据失败');
        }

        return $this->findHomeStencils($homeTemplate->id);
    }

    /**
     * 获取首页分组数据
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    protected function getHomeGroup(int $id)
    {
        $group = $this->homeTemplateRepository->findWhere([
            'id'        =>  $id,
            'parent_id' =>  0,
            'status'    =>  1,
        ]);

        $homeGroup = $group->first();

        if (empty($homeGroup)) {
            throw new NotFoundHttpException('获取首页数据失败');
        }

        return $homeGroup;
    }

    /**
     * 获取首页组内生效的一个首页
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    protected function getHomeParentById(int $id)
    {
        return $this->homeTemplateRepository->findHomeParentById($id);
    }

    /**
     * 获取首页模块
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    protected function findHomeStencils(int $id)
    {
        return $this->homeTemplateStencilRepository->findHomeStencilsById($id);
    }
}